﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using YunNanHealthProject_Register.Entity;

namespace YunNanHealthProject_Register.Model
{
    class UpResourceInfo
    {
        private static string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;

        IDbConnection connection = new SqlConnection(connStr);

        //停诊上传
        public List<UpStopInfo> GetUpStopInfos(string weekDay)
        {
            string sql = @" SELECT '43120477700' AS hosOrgCode ,            
                                 KeyNo AS scheduleId ,                     
                                 KeyNo AS numSourceId ,                    
                                 SUBSTRING(KSCode, 1, 2) AS oneDeptCode ,  
                                 RTRIM(KSCode) AS deptCode ,               
                                 CASE YSCode                               
                                   WHEN '*' THEN '2'                       
                                   ELSE '1'                                
                                 END AS orderType ,                        
                                 CASE YSCode                               
                                   WHEN '*' THEN RTRIM(KSCode)             
                                   ELSE RTRIM(YSCode)                      
                                 END AS resourceCode ,                     
                                 '未上传' AS stopReason                     
                             FROM   dbo.MZSchedule(NOLOCK)                    
                             WHERE  Status = 1                                
                             AND totNum > 0                                   
                             AND( StopBegDate > CONVERT(VARCHAR(10),DATEADD(DAY,2,GETDATE()),120)OR StopEndDate < CONVERT(VARCHAR   (10),DATEADD(DAY,2,GETDATE()),120))
                             AND Weekday = @day";
            return connection.Query<UpStopInfo>(sql, new { day = weekDay }).ToList();
        }

        //替诊上传
        public List<UpReplaceScheduleInf> GetUpReplaceScheduleInfs(string weekDay)
        {
            string sql = @" SELECT '43120477700' AS hosOrgCode ,           
                                 KeyNo AS scheduleId ,                     
                                 KeyNo AS numSourceId ,                    
                                 SUBSTRING(KSCode, 1, 2) AS oneDeptCode ,  
                                 RTRIM(KSCode) AS deptCode ,               
                                 CASE YSCode                               
                                   WHEN '*' THEN '2'                       
                                   ELSE '1'                                
                                 END AS orderType ,                        
                                 CASE YSCode                               
                                   WHEN '*' THEN RTRIM(KSCode)             
                                   ELSE RTRIM(YSCode)                      
                                 END AS resourceCode ,                     
                                 RTRIM(YSName) AS resourceName,            
                                 RTRIM(YSCode2) AS replaceDoctCode,        
                                 RTRIM(YSName2) AS replaceDoctName,        
                                 '未上传' AS replaceReason                  
                             FROM dbo.MZSchedule(NOLOCK)                      
                             WHERE  Status = 2                                
                             AND totNum > 0                                   
                             AND( StopBegDate > CONVERT(VARCHAR(10),DATEADD(DAY,2,GETDATE()),120)OR StopEndDate < CONVERT(VARCHAR   (10),DATEADD(DAY,2,GETDATE()),120))
                             AND Weekday = @day";
            return connection.Query<UpReplaceScheduleInf>(sql, new { day = weekDay }).ToList();
        }

        //预约就诊情况上传
        public List<UpOrderMedicin> GetUpOrderMedicin(string beginDate, string endDate)
        {
            string sql = @" SELECT '43120477700' AS hosOrgCode,                                      
                                    c.OrderId AS orderId,                                              
                           	        CASE b.DiagCode WHEN '' THEN '未填写' ELSE b.DiagCode END AS jzzdbm,
                           	        CASE b.DiagName WHEN '' THEN '未填写' ELSE b.DiagName END AS jzzdmc,
                           	        '01' AS bmlx,                                                      
                           	        CASE a.YSCode WHEN '' THEN '*' ELSE a.YSCode END AS jzysgh,        
                           	        CASE a.YSName WHEN '' THEN '任意医师' ELSE a.YSName END AS jzysxm    
                           FROM  dbo.MZRegisterSheet a(NOLOCK)                                       
                           JOIN dbo.MZDiagnose b(NOLOCK) ON a.MZNum = b.MZNum                        
                           JOIN dbo.AP_ReservationSheet c(NOLOCK) ON a.PatientID = c.PatientID       
                           WHERE a.ReserveFlag = 1                                                   
                           AND a.RegisterDate >= @begin                                              
                           AND a.RegisterDate < @end                                                 
                           AND c.DelDate IS NULL                                                     
                           AND a.MZNum > 0 ";
            return connection.Query<UpOrderMedicin>(sql, new { begin = beginDate, end = endDate }).ToList();
        }


    }
}
